import {
  curTimeAll
} from '../../third-utils/time/index.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
      recordDate:'',
        curCate:'',
        imageAttr:[],
        fs_status:{},
        foodAttr:[''],
        medicines:[],
        statusIndex:-1,
        description:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(e) {

        
      this.setData({
        curCate:e.cateId,
        recordDate:curTimeAll
      })
      this.getCurData(e.cateId)
          this.initData()
          this.getBaby()
    },

    
  getBaby() {


    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/baby/view',
      data: {
        id: 32
      },
      method: 'get',
      success: (res) => {
        console.log(res, 122)
        if (res.data.code == 200) {
          if (res.data.data.foods) {

            this.setData({
              medicines: (res.data.data.foods).split(','),
            })
          }
        }
      }
    })
  },
  saveFood(){
    let addAttr=JSON.parse(JSON.stringify(this.data.foodAttr))
    for(let i in this.data.foodAttr){
      this.data.medicines.some(item=>{
        if(this.data.foodAttr[i]==item){
          addAttr.splice(i,1)
        }
      })
    }
    addAttr.some(item=>{
      
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/baby/addFoods',
      data: {
        babyId: "32",
        foods: item
      },
      method: 'post',
      success: (res) => {
        console.log(res, 122)
        if (res.data.code == 200) {}
      }
    })
    })
  },
	getCurData(id) {

		wx.request({
			url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/findList',
			data: {
				babyId:32,
				// pageSize: 1,
				typeId: id
			},
			method: 'post',
			success: (res) => {
				if (res.data.code == 200) {
					if (res.data.data.list.length > 0) {
						this.data.medicines=[]
						let curData = {}
						curData = res.data.data.list[0].detail
						this.setData({
							medicines: (curData.food).split(','),
						})
					}
				}
			}
		})
	},
  onTextChange(e) {
    this.data.foodAttr[e.currentTarget.dataset.index] = e.detail.value
    this.setData({
      foodAttr: this.data.foodAttr
    })
  },
  delYao(e) {
    console.log(this.data.foodAttr[e.currentTarget.dataset.index])
    this.data.foodAttr.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      foodAttr: this.data.foodAttr
    })
  },
  addYao(e) {
    this.data.foodAttr.push(e.currentTarget.dataset.val || '')
    this.setData({
      foodAttr: this.data.foodAttr
    })
  },
    select(e){
      this.setData({
        [e.currentTarget.dataset.key]:e.currentTarget.dataset.index
      })
    },
    pickerChangeType(e){
      console.log(e,21)
      this.setData({
        [e.currentTarget.dataset.key]:e.detail.value
      })
    },
  formSubmit(e){
    console.log(e,12122)

    if(this.data.foodAttr.length<=0){
      wx.showToast({
        title: '请添加食材',
        icon: 'none',
        duration: 1500 //持续的时间
      })
      return
    }
    let hasNone=false
    this.data.foodAttr.some(item=>{
      if(!item){
        hasNone=true
      }
    })
    if(hasNone){
      wx.showToast({
        title: '请填写食材',
        icon: 'none',
        duration: 1500 //持续的时间
      })
      return

    }
    if(this.data.statusIndex<0){
      wx.showToast({
        title: '请选择辅食状态',
        icon: 'none',
        duration: 1500 //持续的时间
      })
      return
    }
    let post={
      babyId:32,
      typeId:this.data.curCate,
      
    }
      post.detail={
        recordDate:this.data.recordDate,
        food:String(this.data.foodAttr),
        status:this.data.fs_status.opts[this.data.statusIndex].name,
        firstFood:e.detail.value.firstFood,
        description:e.detail.value.description,
        picture:String(this.data.imageAttr)
      }
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/add',
      data:post,
      method: 'post',
      success:  (res)=> {
        console.log(res,122)
        if (res.data.code == 200) {
          this.saveFood()
          wx.showToast({
            title: '保存成功！',
            icon: 'none',
            duration: 1500 //持续的时间
          })
          setTimeout(()=>{

            wx.navigateTo({
              url: '/pages/index/index',
            })
          },1500)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
    changeCate(e){
      this.setData({
        curCate:e.currentTarget.dataset.index
      })
    },
    delImg(e){
      this.data.imageAttr.splice(e.currentTarget.dataset.index,1)
      this.setData({
        imageAttr:this.data.imageAttr
      })
    },
    addImg(){
      wx.chooseImage({
        count:9,
        success: (chooseImageRes) => {
          console.log(chooseImageRes)
          const tempFilePaths = chooseImageRes.tempFilePaths;
          tempFilePaths.some((item)=>{
            wx.uploadFile({
              url: 'https://babyplanet.isv-dev.youzan.com/common/image/upload', //仅为示例，非真实的接口地址
              filePath:item,
              fileType: 'image',
              name: 'file',
              headers: {
                'Accept': 'application/json',
                'Content-Type': 'multipart/form-data',
              },
              success: (uploadFileRes) => {
                let res=JSON.parse(uploadFileRes.data)
                if(res.code==200){
                  this.data.imageAttr.push(res.data.imageUrl)
                  this.setData({
                    imageAttr:this.data.imageAttr
                  })
                }
              },
            });
          })
        }
      });
    },
    initData(){
        let codeAttr=['fs_status']
        codeAttr.some(item=>{
            wx.request({
                url: 'https://babyplanet.isv-dev.youzan.com/api/selectConfig/view',
                data: {
                    code:item
                },
                method: 'get',
                success: (res) => {
                  if (res.data.code == 200) {
                    this.setData({
                        [item]:res.data.data
                    })
                  }
                }
              })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})